#include <memory>
#include <string.h>
#include <unordered_map>
namespace Umikaze::Core {

//需要统一管理location
class GlUniformBocks {
public:
  struct UboCache {
    uint32_t _location;
    void *_data;
    size_t _size;

    bool needUpdate(void *data, size_t size) {
      if (size != _size || data != _data) {
        return true;
      }

      return !memcmp(data, _data, size);
    }
  };

  uint32_t genBuffer();

  void updateBuffer(uint32_t ubo, void *data, size_t size);

  uint32_t getLocation(uint32_t ubo);

  void deleteBuffer(uint32_t ubo) {

    // TODO:
  }

  static const std::unique_ptr<GlUniformBocks> &instance() {
    if (!_instance) {
      _instance = std::unique_ptr<GlUniformBocks>(new GlUniformBocks());
    }
    return _instance;
  }

private:
  GlUniformBocks() = default;

  uint32_t getEmptyLocation() {
    for (uint32_t i = 0; i < 32; ++i) {
      uint32_t temp = emptyLocation >> i;
      if (temp % 2 == 0) {
        emptyLocation += 1 << i;
        return i;
      }
    }
    return 9999999;
  }

  //暂定32个位置
  uint32_t emptyLocation = 0;
  std::unordered_map<uint32_t, UboCache> _uniformBlocks;
  static std::unique_ptr<GlUniformBocks> _instance;
};

} // namespace Umikaze::Core
